package com.rob.banner.client;

import com.google.gwt.animation.client.Animation;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Widget;

public class FadeAnimation extends Animation {

	private Widget in;
	private Widget out;
	public FadeAnimation(Widget in, Widget out) {
		this.in = in;
		this.out = out;
	}
	
	@Override
	protected void onUpdate(double progress) {
		if(in != null) {
			setOpacity(in, progress);
		}
		if(out !=null) {
			setOpacity(out, 1-progress);
		}
	}
	
	@Override
	protected void onComplete() {
		if(out != null) {
			out.setVisible(false);
		}
	}
	
	@Override
	protected void onStart() {
		if(in != null) {
			setOpacity(in, 0);
			in.setVisible(true);
		}
	}
	
	
	public void setOpacity(Widget image, double opacity) {
		Element element = image.getElement();
	  if (opacity > .995) {
	    element.getStyle().setProperty("opacity", "");
	    element.getStyle().setProperty("filter", "");
	  } else {
	    String s = Integer.toString((int) Math.round(opacity * 100));
	    String sDecimal = (s.length() == 1 ? ".0" : ".") + s;
	    element.getStyle().setProperty("opacity", sDecimal);
	    element.getStyle().setProperty("filter", "alpha(opacity=" + s + ")");
	  }
	}
}
